<template>
	 <uni-nav-bar title="确认订单" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
	 <view class="package" style="padding:20rpx 0px">
		<view class="packageLsit">
			<image src="../../static/groupBenefits/shop.png" mode=""></image>
			<text>{{shopName}}</text>
		</view>
		<view v-for="(item,index) in shopAll.goods_list" :key='item.id'>
			<ShopList :type="'num'"  :shoppAll="item" :jump="'1'"></ShopList>
		</view>
		<Shoppay :payFor="shopAll" :state="'sureOrder'"></Shoppay>
	 </view>
	  <ShopBot :title="'提交订单'" :Select="Select" @submit="submit" :total="shopAll.money"
		:Discount="shopAll.yh_money"></ShopBot>
</template>

<script setup lang="ts">
import { navBack } from '@/utils/navigator';
import { errorToast, successToast } from '@/utils/prompt';
import { isH5 } from '@/utils/platform';
import ShopList from '@c/groupBenefits/shopGrid.vue';
import ShopBot from '@c/groupBenefits/shopBot.vue';
import Shoppay from '@c/groupBenefits/pay.vue';
import { onLoad } from '@dcloudio/uni-app';
import { toPublish } from '@mqtt';
import { OrderList } from '@/gql/shop';
import { ref, Ref } from 'vue';
const orderID = ref('');
const shopName = ref('');
const shopAll: Ref<any> = ref({});
const repeat = ref(true); //放重复点击
onLoad((pearm) => {
  orderID.value = pearm.id;
  shopName.value = pearm.shopName;
  init();
});
function init() {
  const payload = {
    query: OrderList,
    variables: { order_id: Number(orderID.value) },
  };
  toPublish('ql/store/getOrderGoodsInfo', payload, (obj: any) => {
    const { order_goods_info } = obj.data;
    shopAll.value = order_goods_info;
  });
}
function submit() {
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  const payload = {
    order_id: orderID.value,
    method: isH5 ? 'mp' : 'mini',
  };
  toPublish('store/payWechatByOrderGoods', payload, (obj: any) => {
    const { code, msg, data } = obj;
    if (code === 1) {
      successToast(msg);
      weach(data);
    } else {
      repeat.value = true;
      errorToast(msg);
    }
  });
}

function weach(num) {
  // @ts-ignore
  WeixinJSBridge.invoke('getBrandWCPayRequest', num, function(res) {
    switch (res.err_msg) {
      case 'get_brand_wcpay_request:ok':
        successToast('支付成功');
        setTimeout(() => {
          uni.redirectTo({
            url: '/pages/shopp/success',
          });
        }, 1500);
        break;
      case 'get_brand_wcpay_request:cancel':
        errorToast('支付取消 请稍后重试');
        setTimeout(() => {
          uni.redirectTo({
            url: '/pages/order/commodityDetail?id=' + orderID.value,
          });
        }, 1500);
        break;
      case 'get_brand_wcpay_request:fail':
        errorToast('支付失败 请稍后重试');
        setTimeout(() => {
          uni.redirectTo({
            url: '/pages/order/commodityDetail?id=' + orderID.value,
          });
        }, 1500);
        break;
    }
  });
}
</script>

<style scoped lang="less">
.packageLsit {
  width: 100%;
  display: flex;
  align-items: center;
  padding: 0px 20rpx;
  box-sizing: border-box;
  image {
    width: 50rpx;
    height: 50rpx;
  }
  text {
    font-size: 30rpx;
    font-weight: 400;
    color: #181818;
    margin: 0px 10rpx;
  }
}
</style>